package com.worldtolearn.order.bu;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.inject.Inject;
import javax.inject.Named;

import org.springframework.beans.factory.annotation.Autowired;

import com.worldtolearn.order.dao.IOrderDAO;
import com.worldtolearn.order.entity.Order;
import com.worldtolearn.order.entity.OrderLine;
import com.worldtolearn.user.entity.Person;
@Named
public class OrderBu implements IOrderBu {
	
	@Inject
	IOrderDAO orderDao;
	
	@Inject
	OrderLine ol;
	
	@Autowired
	Order od ;
	
	Set<OrderLine> set;

	private boolean isOrderok =false;

	@Override
	public void createOrder(Order order) {
		orderDao.createOrder(order);

	}
	
	public void setOrder(List<OrderLine> orderLines, Person person) {
		set = new HashSet<OrderLine>(orderLines);
		double total = 0;
		for (OrderLine orderLine : set) {
			System.out.println("orderLine : "+orderLine);
			total += orderLine.getTotalCost();
		}
		od.setTotalAmount(total);
		System.out.println("total en base : "+total);
		Date date = new Date();
		od.setDate(date);
		od.setOrderLines(set);
		od.setPerson(person);
		System.out.println("trace person en base suite commande ok : "+person.getFirstName());
		setOrderOk(true);
		
	}
	public void createOrderFromBean() {
		orderDao.createOrder(od);
	}

	@Override
	public void deleteOrder(Order order) {
		//TODO delete order
	}

	@Override
	public List<Order> getLastOrder(int idPerson) {
		orderDao.getLastOrder(idPerson).size();
		return orderDao.getLastOrder(idPerson);
	}

	@Override
	public List<OrderLine> getLineOrderByOrder(Order order) {
		orderDao.getLineOrderByOrder( order);
		return null;
	}

	public boolean isOrderOk() {
		return isOrderok;
	}

	public void setOrderOk(boolean isOderOk) {
		this.isOrderok = isOderOk;
	}

}
